use TestHelper;
use EntityTest;
use EntityBagTestHelper;
use MapManager;
use Map;

sub testEntityManagerFields{
    my $mgr = shift;

    print "Probando Campos\n";
    if(ref($mgr->{ENTITIES}) eq "EntityBag"){
	print "    ENTITIES OK\n";
    }else{
	print "--->ERROR: ".(ref($mgr->{ENTITIES}))." ne EntityBag\n";
    }
} # testEntityManagerFields

sub testRegisterToNoCat{
    my $mgr = shift;
    my $entity = shift;

    print "Probando registracion\n";
    
    $mgr->registerEntity($entity);
    
    if($mgr->where($entity) ne $entity->type){
	print "--->ERROR: No encontrado en la categoria correcta: '".$mgr->where($entity)."' ne '".$entity->type."'\n"; 
    }else{
	print "    Categoria por defecto '".$entity->type."' OK\n";
    }
} # testRegisterToNoCat

sub testEntitySpawn{
    my $mgr = shift;
    my $entityClass = shift;

    print "Probando generacion\n";
    my $entity = $mgr->spawnEntity($entityClass);

    testType($entity, $entityClass);

    print ">Ubicacion\n";
    if($mgr->where($entity) ne $entity->type){
	print "--->ERROR: No encontrado en la categoria correcta: '".$mgr->where($entity)."' ne '".$entity->type."'\n"; 
    }else{
	print "    Categoria por defecto '".$entity->type."' OK\n";
    }
    return $entity;
} # testEntitySpawn


sub testEntitySpawnNoRegister{
    my $mgr = shift;
    my $entityClass = shift;

    print "Probando generacion SIN REGISTRACION\n";
    my $entity = $mgr->spawnEntity($entityClass, "UNREGISTERED");

    testType($entity, $entityClass);

    print ">Ubicacion\n";
    if($mgr->where($entity) ne ''){
	print "--->ERROR: Se encontro en la categoria: '".$mgr->where($entity)."'\n";
    }else{
	print "    OK: no se registro\n";
    }
    return $entity;
} # testEntitySpawnNoRegister

sub testRemove{
    my $mgr = shift;
    my $entity = shift;

    print "Probando eliminacion\n";

    $mgr->unregisterEntity($entity);
    if($mgr->where($entity) ne ''){
	print "--->ERROR: Se encontro en la categoria: '".$mgr->where($entity)."'\n";
    }else{
	print "    OK: Se elimino\n";
    }
}

sub testRegisterToCategory{
    my $mgr = shift;
    my $entity = shift;
    my $cat = shift;

    print "Probando registracion a categoria $cat\n";
    $mgr->registerEntity($entity, $cat);
    
    if($mgr->where($entity) ne $cat){
	print "--->ERROR: No encontrado en la categoria correcta: '".$mgr->where($entity)."' ne '".$cat."'\n"; 
    }else{
	print "    OK: Encontrado en $cat\n";
    }
} # testRegisterToCategory


sub testSpawnIntoCategory{
    my $mgr = shift;
    my $type = shift;
    my $cat = shift;

    print "Probando generacion de $type y registracion en categoria $cat\n";
    my $entity =  $mgr->spawnEntity($type, $cat);
    
    if($mgr->where($entity) ne $cat){
	print "--->ERROR: No encontrado en la categoria correcta: '".$mgr->where($entity)."' ne '".$cat."'\n"; 
    }else{
	print "    OK: Encontrado en $cat\n";
    }
} # testRegisterToCategory


sub testFullEntityList{
    my $mgr = shift;
    my $listref = shift;
    print "Probando contenido Total\n";
    testEntityList($mgr, $listref);
} # testFullEntityList

sub testCategoryEntityList{
    my $mgr = shift;
    my $catName = shift;
    my $listref = shift;

    print "Probando contenido en categoria $catName\n";
    testEntityList($mgr, $listref, $catName);
} # testCategoryEntityList

sub testEntityList{
    my $mgr = shift;
    my $listref = shift;
    my $catname = shift;

    my @list = @{$listref};

    # foreach my $a (@list){print($a." <<<\n");}

    
    my @all = @{$mgr->entities($catname)};
    my $errAll = 0;
    my $notFound;
    my $stop;
    my $i;
    foreach (@all){
	$notFound = 1;
	$stop = 0;
	$i = 0;
	while($notFound && !$stop){
	     #print "$i - [[".$list[$i]." - ".$_."]]\n";
	    if($list[$i] eq $_){
		$notFound = 0;
	    }
	    $i++;
	    if($i > scalar(@list)){
		$stop = 1;
	    }
	}
	if($notFound){
	    $errAll = 1;
	    print "--->ERROR: No se encontro un elemento esperado:".$_."\n";
	}
    }

    if(!$errAll){
	if(scalar(@list) == scalar(@all)){
	    print "    Contenido total OK\n";
	}else{
	    print "--->ERROR: Cantidad total distinta: esperada = ".scalar(@list)."; encontrada = ".scalar(@all)."\n";
	}
    }
} # testEntityList
1;
